package pro.shushi.pamirs.demo.core.init;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.boot.common.api.command.AppLifecycleCommand;
import pro.shushi.pamirs.boot.common.api.init.LifecycleCompletedAllInit;
import pro.shushi.pamirs.boot.common.extend.MetaDataEditor;
import pro.shushi.pamirs.core.common.InitializationUtil;
import pro.shushi.pamirs.demo.api.DemoModule;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.api.dto.meta.Meta;
import pro.shushi.pamirs.meta.domain.module.ModuleDefinition;
import pro.shushi.pamirs.metadata.manager.core.helper.DesignerInstallHelper;
import pro.shushi.pamirs.metadata.manager.spi.MetaInstallConfigApi;

import java.util.Map;


/**
 * 安装设计器导出的元数据
 */
@Slf4j
@Component
public class DemoModuleAppInstall implements MetaDataEditor, LifecycleCompletedAllInit, MetaInstallConfigApi {

    private static final String INSTALL_META_PATH = "";
//    private static final String INSTALL_META_PATH = "install/meta.json";
//    private static final String INSTALL_WIDGET_ZIP_PATH = "install/widget.zip";
//    private static final String INSTALL_WIDGET_JSON_PATH = "install/widget.json";

    @Override
    public void edit(AppLifecycleCommand command, Map<String, Meta> metaMap) {
        if(StringUtils.isBlank(INSTALL_META_PATH)) return;
//        log.info("开始安装demo_core应用-元数据");
        try {
//            log.info("开始安装DemoModule应用界面设计元数据");
            InitializationUtil util = InitializationUtil.get(metaMap, DemoModule.MODULE_MODULE, DemoModule.MODULE_NAME);
            if (null != util) {
                // 设计器的元数据
                DesignerInstallHelper.mateInitialization(util, INSTALL_META_PATH, DemoModule.MODULE_MODULE,
                        DemoModule.MODULE_NAME);
            }
        } catch (Exception e) {
//            log.error("初始化界面设计器导入异常", e);
        }

//        log.info("开始安装demo_core应用-自定义组件");
//        DesignerInstallHelper.mateInitialization(util, INSTALL_WIDGET_JSON_PATH);
    }


    @Override
    public void process(AppLifecycleCommand command, Map<String, ModuleDefinition> runModuleMap) {
        if(StringUtils.isBlank(INSTALL_META_PATH)) return;
//        log.info("开始安装demo_core应用-设计器数据");
        // 支持远程调用,但是执行的生命周期必须是LifecycleCompletedAllInit或之后. 本地如果安装了设计器,则没有要求
        DesignerInstallHelper.bizInitialization(INSTALL_META_PATH);

        //log.info("开始安装demo_core应用-设计器自定义组件");
        // 当开发环境和导入环境的文件服务不互通时, 可通过指定js和css的文件压缩包,自动上传到导入环境,并替换导入组件数据中的文件url
        //WidgetInstallHelper.bizInitialization(INSTALL_WIDGET_JSON_PATH, INSTALL_WIDGET_ZIP_PATH);

        //WidgetInstallHelper.bizInitialization(INSTALL_WIDGET_JSON_PATH);
    }

    @Override
    public Boolean installForDesigner() {
        return Boolean.TRUE;
    }
}
